package com.woniuxy.dudugua.controller;

import com.woniuxy.dudugua.entity.Order;
import com.woniuxy.dudugua.entity.ReturnGoods;
import com.woniuxy.dudugua.entity.User;
import com.woniuxy.dudugua.mapper.OrderMapper;
import com.woniuxy.dudugua.service.ReturnGoodsService;
import com.woniuxy.dudugua.util.Result;
import com.woniuxy.dudugua.util.view.ReturnGoodsAndProductView;
import com.woniuxy.dudugua.util.view.ReturnProductView;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("returngoods")
public class ReturnGoodsController {
    @Autowired
    private ReturnGoodsService returnGoodsService;
    @RequestMapping("getproducts")
    public Result getProductsByOrderId(int orderid){
        List<ReturnProductView> list = returnGoodsService.selectProductByOrderId(orderid);
        return new Result("200","success",null,list);
    }
    @RequestMapping("insert")
    public Result insert(ReturnGoods returnGoods){
        String code = "";
        ReturnGoods re = returnGoodsService.selectByOrderId(returnGoods.getOrderId());
        if(returnGoods.getOrderId() == 0){
            code = "406";
        }
        else if(returnGoods.getReason() == null || returnGoods.getReason() == ""){
            code = "407";
        }
        else if(re != null){
            code = "405";
        }else{
            User user = (User)SecurityUtils.getSubject().getPrincipal();
            returnGoods.setUserId(user.getId());
            returnGoods.setApplyTime(new Date());
            returnGoodsService.insertReturnGoods(returnGoods);
            code = "200";
        }
        return new Result(code,"success",null,null);
    }
    @RequestMapping("getreturnlist")
    public Result getReturnList(){
        User user = (User)SecurityUtils.getSubject().getPrincipal();
        List<ReturnGoodsAndProductView> list = returnGoodsService.selectByUserId(user.getId());
        return new Result("200","success",null,list);
    }
    @RequestMapping("getreturnbyid")
    public Result getReturnByID(int id){
        ReturnGoodsAndProductView r = returnGoodsService.selectReturnProductViewByReturnId(id);
        return new Result("200","success",r,null);
    }
    @RequestMapping("getorderbyreturnid")
    public Result getOrderByReturnId(int returnid){
        Order order = returnGoodsService.selectOrderByReturnId(returnid);
        return new Result("200","success",order,null);
    }
    @RequestMapping("updatetracknumber")
    public Result updateTrackingNumber(int returnId,String trackNumber){
        int i = returnGoodsService.updateTrackNumberByReturnId(returnId, trackNumber);
        return new Result("200","success",null,null);
    }
}
